// import { Module } from 'vuex';
import { Session } from "@/utils/storage";
import { login, logout, getInfo } from "@/api/login";
import { getToken, removeToken } from "@/utils/auth";
// 此处加上 `.ts` 后缀报错，具体原因不详
// import { UserInfosState, RootStateTypes } from '/@/store/interface/index';

const userInfosModule = {
  namespaced: true,
  state: {
    userInfos: {},
    token: getToken(),
    name: "",
    avatar: "",
    roles: [], // 角色 admin,test,...
    permissions: [], // 权限点 sys.btn.add,sys.btn.delete,...
  },
  mutations: {
    // 设置用户信息
    getUserInfos(state, data) {
      state.userInfos = data;
    },

    // 设置 token
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    // 设置名称
    SET_NAME: (state, name) => {
      state.name = name;
    },
    // 设置头像
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    // 设置角色
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    // 设置权限
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions;
    },
  },
  actions: {
    // 设置用户信息
    setUserInfos({ commit }, data) {
      //debugger;
      if (data) {
        commit("getUserInfos", data);
      } else {
        if (Session.get("userInfo")) {
          commit("getUserInfos", Session.get("userInfo"));
        }
      }
    },

    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {
        login({ username, password, code,uuid })
          .then((res) => {
            //("登录接口返回信息:", res);
            // 将token信息保存到session会话中
            Session.set("token", res.token);
            commit("SET_TOKEN", res.token);
            resolve();            
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 获取用户信息
    GetInfo({ commit }) {
      //("获取用户信息");
      return new Promise((resolve, reject) => {
        getInfo()
          .then((res) => {
           // console.log(res);
            const user = res.user;
            const avatar =
              user.avatar == ""
                ? require("@/assets/images/profile.png")
                : process.env.VUE_APP_BASE_API + user.avatar;
            if (res.roles && res.roles.length > 0) {
              // 验证返回的roles是否是一个非空数组
              commit("SET_ROLES", res.roles);
              commit("SET_PERMISSIONS", res.permissions);
            } else {
              commit("SET_ROLES", ["ROLE_DEFAULT"]);
            }
            commit("SET_NAME", user.userName);
            commit("SET_AVATAR", avatar);
            const userInfo = {
              token: getToken(),
              roles: res.roles,
              avatar:
                require("@/assets/images/profile.gif"),
              name: user.userName,
              permissions: res.permissions,
              indexPage: "/index",
            };
            commit("getUserInfos", userInfo);
            Session.set('userInfo', userInfo);
            resolve(res);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token)
          .then(() => {
            commit("SET_TOKEN", "");
            commit("SET_ROLES", []);
            commit("SET_PERMISSIONS", []);
            removeToken();
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise((resolve) => {
        commit("SET_TOKEN", "");
        removeToken();
        resolve();
      });
    },
  },
};

export default userInfosModule;
